<?php

/**
 * Created by PhpStorm.
 * User: Celebration-Grey
 * Date: 2017/4/25
 * Time: 21:01
 */
namespace App\Http\Controllers\Follow;
use App\Http\Classes\Follow;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Classes\Log;

class FollowController extends Controller
{
    public function followUser(Request $request){
        try{
            if($request->has('uid')){
                $dataArray = [];
                $dataArray['followed_user_id'] = $request->input('uid');
                $dataArray['following_user_id'] = $_COOKIE['user_id'];
                if(Follow::follow($dataArray)){
                    return $this->response(200,"关注成功！",null);
                }
                else{
                    return $this->response(304,"关注失败！您可能已关注了此用户",null);
                }
            }
            else{
                return $this->response(503,"缺少必要参数！",null);
            }
        }
        catch (\Exception $exception){
            Log::userError(__FUNCTION__,$exception->getCode(),$exception->getMessage(),$exception->getFile(),$exception->getLine(),isset($_COOKIE['user_id']) ? $_COOKIE['user_id'] : 0);
            return $this->response(500,"系统内部错误！".$exception->getMessage(),null);
        }
    }

    public function unfollowUser(Request $request){
        try{
            if($request->has('uid')){
                $dataArray = [];
                $dataArray['followed_user_id'] = $request->input('uid');
                $dataArray['following_user_id'] = $_COOKIE['user_id'];
                if(Follow::unfollow($dataArray)){
                    return $this->response(200,'取关成功！',null);
                }
                else{
                    return $this->response(304,'取关失败！',null);
                }
            }
            else{
                return $this->response(503,"缺少必要参数！",null);
            }
        }
        catch (\Exception $exception){
            Log::userError(__FUNCTION__,$exception->getCode(),$exception->getMessage(),$exception->getFile(),$exception->getLine(),isset($_COOKIE['user_id']) ? $_COOKIE['user_id'] : 0);
            return $this->response(500,"系统内部错误！".$exception->getMessage(),null);
        }
    }

    private function response($status,$message,$data){
        $response = [];
        $response['status'] = $status;
        $response['message'] = $message;
        $response['data'] = $data;
        return response()->json($response);
    }
}